﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Hxj.Data;
using BaseUI;
using Utility;
using Utility.Model;

namespace MsgCenter.Web.Pages.Templet
{
    public partial class Option : BasePage
    {
        protected string strType = ParamsOfRequest.Request("type");
        protected string strGuID = ParamsOfRequest.Request("Guid");
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                if (strType == "Add")
                {
                    this.linkSave.Visible = true;
                }
                else if (strType == "Edit")
                {
                    this.linkSave.Visible = true;
                    this.SetModelValue();
                }
                else if (strType == "Look")
                {
                    this.SetModelValue();
                    ConvertHelper.ToReadOnly(txtCode, txtName);
                    this.txtDes.ToReadOnly();
                }
            }
        }

        private void SetModelValue()
        {
            Sys_Templet model = BLL.BLLServiceHelper.GetBaseDataService().GetModel<Sys_Templet>(Sys_Templet._.TempID == strGuID);
            if (model != null)
            {
                txtCode.Value = model.TempAppCode;
                txtName.Value = model.Title;
                txtDes.Text = model.TempletDes;
                txtAliCode.Value = model.AliTempletCode;
            }
        }

        protected void linkSave_Click(object sender, EventArgs e)
        {
            if (strType == "Edit")
            {
                try
                {
                    Sys_Templet model = BLL.BLLServiceHelper.GetBaseDataService().GetModel<Sys_Templet>(Sys_Templet._.TempID == strGuID);
                    if (model != null)
                    {
                        model.TempAppCode = txtCode.Value;
                        model.Title = txtName.Value;
                        model.TempletDes = txtDes.Text;
                        model.CreateUser = base.Account;
                        model.AliTempletCode = txtAliCode.Value;
                        BLL.BLLServiceHelper.GetRoleService().Save(model);
                        base.ShowAlertScript("Close_Reload();");
                    }
                    else
                    {
                        throw new Exception("获取修改对象实体为空");
                    }
                }
                catch (Exception ex)
                {
                    base.ShowAlert("保存数据出错:" + ex.Message);
                }
            }
            else if (strType == "Add")
            {
                try
                {
                    Sys_Templet model = BLL.BLLServiceHelper.GetBaseDataService().GetModel<Sys_Templet>(Sys_Templet._.TempAppCode == txtCode.Value);
                    if (model == null)
                    {
                        model = new Sys_Templet();
                        model.TempAppCode = txtCode.Value;
                        model.Title = txtName.Value;
                        model.TempletDes = txtDes.Text;
                        model.CreateUser = base.Account;
                        model.AliTempletCode = txtAliCode.Value;
                        BLL.BLLServiceHelper.GetRoleService().Save(model);
                        base.ShowAlertScript("Close_Reload();");
                    }
                    else
                    {
                        base.ShowAlert("保存数据失败:输入的模板代码已经存在,请重新输入");
                    }
                }
                catch (Exception ex)
                {
                    base.ShowAlert("保存数据出错:" + ex.Message);
                }
            }
        }
    }
}